package com.school.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

//对应s_student的所有操作
//返回结果的方式
public class StudentDao {
	// CRUD
	// 改
	public int update(Student s) {
		String sql = "update s_student set sname=?,sex=?,telephone=?,qq=?,wechat=?,gradeid=?,enterdate=str_to_date(?,'%Y-%m-%d'),fromcity=?,birthday=str_to_date(?,'%Y-%m-%d'),address=? where stuno=?";
		try {
			Connection connection = DBUtil.getConnection();
			PreparedStatement pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, s.getSname());
			pstmt.setString(2, s.getSex());
			pstmt.setString(3, s.getTelephone());
			pstmt.setString(4, s.getQq());
			pstmt.setString(5, s.getWechat());
			pstmt.setInt(6, s.getGradeid());
			pstmt.setString(7, s.getEnterdate());
			pstmt.setString(8, s.getFromcity());
			pstmt.setString(9, s.getBirthday());
			pstmt.setString(10, s.getAddress());
			pstmt.setInt(11, s.getStuno());
			return pstmt.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		} finally {
			DBUtil.close();
		}
	}

	// 删
	public int del(int stuno) {
		String sql = "delete from s_student where stuno=?";
		try {
			Connection connection = DBUtil.getConnection();
			PreparedStatement pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, stuno);
			return pstmt.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		} finally {
			DBUtil.close();
		}
	}

	// 增
	public int add(Student s) {
		String sql = "insert into s_student(sname,sex,telephone,qq,wechat,gradeid,enterdate,fromcity,birthday,address) values(?,?,?,?,?,?,str_to_date(?,'%Y-%m-%d'),?,str_to_date(?,'%Y-%m-%d'),?)";
		try {
			Connection connection = DBUtil.getConnection();
			PreparedStatement pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, s.getSname());
			pstmt.setString(2, s.getSex());
			pstmt.setString(3, s.getTelephone());
			pstmt.setString(4, s.getQq());
			pstmt.setString(5, s.getWechat());
			pstmt.setInt(6, s.getGradeid());
			pstmt.setString(7, s.getEnterdate());
			pstmt.setString(8, s.getFromcity());
			pstmt.setString(9, s.getBirthday());
			pstmt.setString(10, s.getAddress());
			return pstmt.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		} finally {
			DBUtil.close();
		}
	}

	// >>>R
	// 1:查询全部
	public List<Student> search() {
		List<Student> datas = new java.util.ArrayList<>();
		try (Connection con = DBUtil.getConnection();
				PreparedStatement ps = con.prepareStatement("select * from s_student");
				ResultSet rs = ps.executeQuery();) {
			while (rs.next()) {
				datas.add(new Student(rs.getInt("stuno"), rs.getString("sname"), rs.getString("sex"),
						rs.getString("telephone"), rs.getString("qq"), rs.getString("wechat"), rs.getInt("gradeid"),
						rs.getString("enterdate"), rs.getString("fromcity"), rs.getString("birthday"),
						rs.getString("address")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return datas;
	}

	// 分页查询
	public List<Student> search(int pageNo, int pageSize) {
		List<Student> datas = new java.util.ArrayList<>();
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement ps = con.prepareStatement("select * from s_student order by stuno asc limit ?,?;");
			int offset = (pageNo - 1) * pageSize;
			offset = Math.max(offset, 0);
			ps.setInt(1, offset);
			ps.setInt(2, pageSize);
			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				datas.add(new Student(rs.getInt("stuno"), rs.getString("sname"), rs.getString("sex"),
						rs.getString("telephone"), rs.getString("qq"), rs.getString("wechat"), rs.getInt("gradeid"),
						rs.getString("enterdate"), rs.getString("fromcity"), rs.getString("birthday"),
						rs.getString("address")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} // close
		return datas;
	}

	// 查询单个:找不到是null,找到了,返回一个对象
	public Student get(int id) {
		String sql = "select * from s_student  where stuno=?";
		try {
			Connection connection = DBUtil.getConnection();
			PreparedStatement pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, id);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				return new Student(rs.getInt("stuno"), rs.getString("sname"), rs.getString("sex"),
						rs.getString("telephone"), rs.getString("qq"), rs.getString("wechat"), rs.getInt("gradeid"),
						rs.getString("enterdate"), rs.getString("fromcity"), rs.getString("birthday"),
						rs.getString("address"));
			} else {
				return null;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			DBUtil.close();
		}
	}
}
